package captchaStore

import (
	"CampusCloudAid/constant"
	"CampusCloudAid/dao/redis"
	"context"
	"log"
	"time"
)

var ctx = context.Background()

type RedisStore struct{}

// Set 存验证码
func (r RedisStore) Set(id string, value string) (err error) {
	key := constant.LoginCode + id
	err = redis.Client.Set(ctx, key, value, time.Minute*5).Err()
	if err != nil {
		log.Println(err.Error())
		return
	}
	return
}

// Get 获取验证码
func (r RedisStore) Get(id string, clear bool) string {
	key := constant.LoginCode + id
	val, err := redis.Client.Get(ctx, key).Result()
	if err != nil {
		return ""
	}
	return val
}

// Delete 只要使用过验证码就立即销毁；
func (r RedisStore) Delete(id string) error {
	key := constant.LoginCode + id
	return redis.Client.Del(ctx, key).Err()
}

// Verify 检验验证码
func (r RedisStore) Verify(id, answer string, clear bool) bool {
	v := RedisStore{}.Get(id, clear)
	_ = RedisStore{}.Delete(id)
	return v == answer
}

//// Delete 删除验证码
//func (r RedisStore) Delete(id string) string {
//	id = ""
//	return id
//}
